﻿using System;
using System.Windows;

using EnvDTE;

namespace FanaticToy.CommandCore
{
    public class SingletonCreator
    {
        public void Execute(DTE dte)
        {
            try
            {
                ExecuteCore(dte);
            }
            catch (Exception ex)
            {
                Clipboard.SetText($@"/*
{ex.Message}
*/");
            }

            // 从当前选区的代码移动到指定的地方。
        }

        // 1. 获得当前方法片段
        // 2. 新建指定的转换器，其实我比较想做一个创建 静态实例的功能
        private void ExecuteCore(DTE dte)
        {
            // 找到类，然后使用 Lazy<ClassName> _className 去创建一个延迟的静态实例类。
            var path = dte.ActiveDocument.FullName;
            var classId = Core.Utils.ClassMetaUtils.GetClassId(path);
            if (string.IsNullOrWhiteSpace(classId)) { Clipboard.SetText("// classId not found"); return; }

            var gencode =
$@"#region singleton

private static Lazy<{classId}> _instance = new Lazy<{classId}>();
public static {classId} Instance => _instance.Value;

private static {classId} CreateSingleton()
{{
    return new {classId}();
}}

#endregion";

            Clipboard.SetText(gencode);

        }

        #region singleton

        private static Lazy<SingletonCreator> _instance = new Lazy<SingletonCreator>(CreateSingleton);
        public static SingletonCreator Instance => _instance.Value;
        private static SingletonCreator CreateSingleton()
        {
            return new SingletonCreator();
        }


        #endregion



    }
}
